//$URL: http://dasding.googlecode.com/svn/trunk/api/src/main/java/de/piratech/dasding/api/rest/AbstractResource.java $
//$Id: AbstractResource.java 127 2013-04-07 20:43:54Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.api.rest;

import java.security.Principal;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import de.piratech.dasding.data.User;
import de.piratech.dasding.data.database.ActionCategoryData;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.Database;
import de.piratech.dasding.data.database.EventCategoryData;
import de.piratech.dasding.data.database.EventData;
import de.piratech.dasding.data.database.LocationData;
import de.piratech.dasding.data.database.UserData;

/**
 * AbstractResource with helpers useful for other resources
 * 
 * @author deveth0
 */
public abstract class AbstractResource {

  @Autowired
  private Database mDatabase;
  @Context
  private UriInfo mUriInfo;

  protected ActionCategoryData getActionCategoryData() {
    return mDatabase.getActionCategoryData();
  }

  protected ActionData getActionData() {
    return mDatabase.getActionData();
  }

  protected EventCategoryData getEventCategoryData() {
    return mDatabase.getEventCategoryData();
  }

  protected EventData getEventData() {
    return mDatabase.getEventData();
  }

  protected UserData getUserData() {
    return mDatabase.getUserData();
  }

  protected LocationData getLocationData() {
    return mDatabase.getLocationData();
  }

  protected abstract Logger getLogger();

  protected User getUserFromSecurityContext(SecurityContext pSecurityContext) {
    if (pSecurityContext != null) {
      Principal principle = pSecurityContext.getUserPrincipal();
      if (principle != null) {
        try {
          return (User) principle;
        } catch (ClassCastException cce) {
          getLogger().debug("Couldn't cast principle to user", cce);
        }
      }
    }
    return null;
  }

  protected WebApplicationException getBadRequestException(Exception pException) {
    getLogger().info("BadRequestException caused by ", pException);
    return new WebApplicationException(getBadRequestResponse(pException));
  }

  protected WebApplicationException getBadRequestException(String pMessage) {
    getLogger().info("BadRequestException caused by ", pMessage);
    return new WebApplicationException(getBadRequestResponse(pMessage));
  }

  protected Response getBadRequestResponse() {
    getLogger().info("BadRequestResponse");
    return getBadRequestResponse("Invalid request");
  }

  protected Response getBadRequestResponse(Exception pException) {
    getLogger().info("BadRequestResponse caused by", pException);
    return getBadRequestResponse(pException.getMessage());
  }

  protected Response getBadRequestResponse(String pMessage) {
    getLogger().info("BadRequestResponse {}", pMessage);
    ResponseBuilder rb = Response.status(Response.Status.BAD_REQUEST);
    if (StringUtils.isNotBlank(pMessage)) {
      rb.entity(pMessage);
    }
    return rb.build();
  }

  protected Response getCreatedResponse(String pLocation) {
    getLogger().debug("CreatedResponse {}", pLocation);
    return Response.created(
        mUriInfo.getBaseUriBuilder().path(getClass()).path(pLocation).build())
        .build();
  }

  protected Response getNoContentResponse() {
    getLogger().debug("NoContentResponse");
    return Response.noContent().build();
  }

  protected WebApplicationException getForbiddenException(String pMessage) {
    getLogger().info("BadRequestException caused by ", pMessage);
    return new WebApplicationException(getForbiddenResponse(pMessage));
  }

  protected Response getForbiddenResponse() {
    getLogger().info("ForbiddenResponse");
    return getForbiddenResponse("");
  }

  protected Response getForbiddenResponse(String pMessage) {
    ResponseBuilder rb = Response.status(Response.Status.FORBIDDEN);
    if (StringUtils.isNotBlank(pMessage)) {
      rb.entity(pMessage);
    }
    return rb.build();
  }

  // for test, do not know yet how to add @Context annotated beans
  protected void setUriInfo(UriInfo uriInfoTestImpl) {
    this.mUriInfo = uriInfoTestImpl;
  }
}
